package yxy.game.pm2.command.hero;

import com.alibaba.fastjson.JSON;
import org.redisson.api.RLock;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.herodata;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.module.hero.HeroHandle;
import yxy.game.pm2.module.resonate.ResonateHandle;

import java.util.concurrent.TimeUnit;

/**
 * 武将转移接口
 *
 * @ClassName CMD_HeroTransfer
 * @Author jingtao-zheng
 * @Date 2022/9/16 18:16
 * @Version 1.0
 **/
@MessageType(proto = Command.HeroTransfer, classtype = HeroMessage.HeroTransferRequest.class)
public class CMD_HeroTransfer extends Command<HeroMessage.HeroTransferRequest> {
    @Override
    public void execute(MessageSource messageSource, HeroMessage.HeroTransferRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        HeroMessage.HeroTransferResponse.Builder response = HeroMessage.HeroTransferResponse.newBuilder();
        RLock redisLock = Hero.getRedisLock(Hero.lockKey(playerCode));
        try {
            String srcCode = request.getSrcCode();//源武将编号
            String targetCode = request.getTargetCode();//目标武将编号
            response.setSrcCode(srcCode);
            response.setTargetCode(targetCode);
            if (redisLock.tryLock(5, 10, TimeUnit.SECONDS)) {
                try {
                    Hero srcHero = Hero.get(playerCode, srcCode);
                    Hero targetHero = Hero.get(playerCode, targetCode);
                    Hero tempHero = JSON.parseObject(JSON.toJSONString(srcHero), Hero.class);

                    //资质限制-低资质或者同资质武将才能给高资质转换等级和装备
                    int srcAptitude = DataCenter.getData(srcHero.getDataId(), herodata.class).getAptitude();
                    int targetAptitude = DataCenter.getData(targetHero.getDataId(), herodata.class).getAptitude();
                    if(srcAptitude>targetAptitude){
                        send(messageSource, response, Result.HeroTransferFailAptitude);
                        return ;
                    }

                    //武将等级限制
                    int level = targetHero.getLevel();
                    if (!(level == 1)) {
                        send(messageSource, response, Result.HeroTransferFailLevel);
                        return ;
                    }

                    //共鸣的武将不能转移
                    Result resultForResonate = ResonateHandle.heroIsResonateHero(srcHero, playerCode);
                    if (!resultForResonate.equals(Result.Success)) {
                        send(messageSource, response, resultForResonate);
                        return;
                    }

                    //共鸣的武将不能转移
                    Result resultForResonate2 = ResonateHandle.heroIsResonateHero(targetHero, playerCode);
                    if (!resultForResonate2.equals(Result.Success)) {
                        send(messageSource, response, resultForResonate2);
                        return;
                    }


                    //1.交换等级
                    srcHero.setLevel(targetHero.getLevel());
                    targetHero.setLevel(tempHero.getLevel());
                    //2.交换装备
                    srcHero.setEquips(targetHero.getEquips());
                    targetHero.setEquips(tempHero.getEquips());
                    //3.交换魂玉A&B
                    srcHero.setSoulA(targetHero.getSoulA());
                    targetHero.setSoulA(tempHero.getSoulA());
                    srcHero.setSoulB(targetHero.getSoulB());
                    targetHero.setSoulB(tempHero.getSoulB());
                    //4.交换阶级
                    srcHero.setStage(targetHero.getStage());
                    targetHero.setStage(tempHero.getStage());
                    HeroHandle heroHandle = new HeroHandle();
                    heroHandle.updateHero(playerCode, srcHero);
                    heroHandle.updateHero(playerCode, targetHero);

                    //判断如果是共鸣系统场上的武将，转移系列操作srcHero
                    ResonateHandle.heroIsAboveHeroAndWeaken(srcHero, playerCode);//高等级
                    ResonateHandle.heroIsAboveHeroAndPromote(targetHero, playerCode);//1级


                    send(messageSource, response, Result.Success);
                } catch (Exception e) {
                    e.printStackTrace();
                    send(messageSource, response, Result.DataError);
                } finally {
                    redisLock.unlock();
                }
            }else{
                send(messageSource, response, Result.DataError);
            }
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, HeroMessage.HeroTransferResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
